package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.UserService;
import com.itheima.utils.MD5Utils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;

    //获取当前登录用户的用户名
    @RequestMapping("/getUsername")
    public Result getUsername() throws Exception {
        try {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user.getUsername());
        } catch (Exception e) {
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult page = userService.findPage(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString()
        );
        return page;
    }


    @RequestMapping("/add")
    public Result add(Integer[] roleIds,@RequestBody com.itheima.pojo.User user) {
        try {
            String username = user.getUsername();
            com.itheima.pojo.User userByUsername = userService.findByUsername(username);
            if (userByUsername != null){
                return new Result(false, "用户名已存在,不能重复添加");
            }
            String password = user.getPassword();
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            String encodePW = encoder.encode(password);
            /*String md5Password = MD5Utils.md5(password);*/
            user.setPassword(encodePW);
            userService.add(roleIds,user);
            return new Result(true, MessageConstant.ADD_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_USER_FAIL);
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer userId) {
        try {
            com.itheima.pojo.User user = userService.findById(userId);
            return  new Result(true, MessageConstant.GET_USER_SUCCESS,user);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.GET_USER_FAIL);
        }
    }

    @RequestMapping("/update")
    public Result update(Integer[] roleIds,@RequestBody com.itheima.pojo.User user) {
        try {
            String password = user.getPassword();
            if (password.length() <= 16) {
                BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
                String encodePW = encoder.encode(password);
                user.setPassword(encodePW);
            }
            userService.update(roleIds,user);
            return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_USER_FAIL);
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer userId) {
        try {
            userService.delete(userId);
            return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
    }
}
